package com.study.mybatis.executor.parameter.defaults;

import com.study.mybatis.executor.parameter.ParameterHandler;
import com.study.mybatis.mapping.MappedStatement;
import com.study.mybatis.session.Configuration;
import com.study.mybatis.type.JdbcType;
import com.study.mybatis.type.TypeException;
import com.study.mybatis.type.TypeHandler;
import com.study.mybatis.type.TypeHandlerRegistry;

import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * @author yuantongqin
 * 2021/8/2
 **/
public class DefaultParameterHandler implements ParameterHandler {


    private final TypeHandlerRegistry typeHandlerRegistry;
    private final MappedStatement mappedStatement;
    private final Object parameterObject;
    private final Configuration configuration;

    public DefaultParameterHandler(MappedStatement mappedStatement, Object parameterObject) {
        this.mappedStatement = mappedStatement;
        this.configuration = mappedStatement.getConfiguration();
        this.typeHandlerRegistry = mappedStatement.getConfiguration().getTypeHandlerRegistry();
        this.parameterObject = parameterObject;
    }

    @Override
    public void setParameters(PreparedStatement ps, Object parameter) throws SQLException {


        try {

            if (null != parameter) {
                if (parameter.getClass().isArray()) {
                    Object[] params = (Object[]) parameter;
                    for (int i = 0; i < params.length; i++) {
                        //Mapper保证传入参数类型匹配，这里就不做类型转换了
                        ps.setObject(i + 1, params[i]);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public void aa() {


//        List<ParameterMapping> parameterMappings = mappedStatement.getParameterMappings();
//        if (parameterMappings != null) {
//            for (int i = 0; i < parameterMappings.size(); i++) {
//                ParameterMapping parameterMapping = parameterMappings.get(i);
//                if (parameterMapping.getMode() != ParameterMode.OUT) {
//                    Object value;
//                    String propertyName = parameterMapping.getProperty();
//                    TypeHandler<?> handler = typeHandlerRegistry.getTypeHandler(parameterObject.getClass(), null);
//                    if (handler != null) {
//                        value = parameterObject;
//                    } else {
//                        MetaObject metaObject = configuration.newMetaObject(parameterObject);
//                        value = metaObject.getValue(propertyName);
//                    }
//                    TypeHandler typeHandler = parameterMapping.getTypeHandler();
//
//                    try {
//                        typeHandler.setParameter(ps, i + 1, value);
//                    } catch (TypeException | SQLException e) {
//                        throw new TypeException("Could not set parameters for mapping: " + parameterMapping + ". Cause: " + e, e);
//                    }
//                }
//            }
//        }
    }
}
